<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>按度量排序 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/_sorting_by_a_metric.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/_sorting_by_a_metric.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/_sorting_by_a_metric.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/_sorting_by_a_metric.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="aggregations.html">聚合</a></span>
»
<span class="breadcrumb-link"><a href="_sorting_multivalue_buckets.html">多桶排序</a></span>
»
<span class="breadcrumb-node">按度量排序</span>
</div>
<div class="navheader">
<span class="prev">
<a href="_intrinsic_sorts.html">« 内置排序</a>
</span>
<span class="next">
<a href="_sorting_based_on_deep_metrics.html">基于“深度”度量排序 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="_sorting_by_a_metric"></a>按度量排序 (Sorting by a Metric)<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/300_Aggregations/50_sorting_ordering.asciidoc">edit</a>
</h2>
</div></div></div>
<p>有时，我们会想基于度量(mertic)计算的结果进行排序。
在我们的汽车销售分析仪表盘中，我们可能想按照汽车颜色创建一个销售条形图，但按照汽车平均售价的升序进行排序。</p>
<p>我们可以增加一个度量，再指定 <code class="literal">order</code> 参数引用这个度量即可：</p>
<div class="pre_wrapper lang-sense">
<pre class="programlisting prettyprint lang-sense">GET /cars/transactions/_search
{
    "size" : 0,
    "aggs" : {
        "colors" : {
            "terms" : {
              "field" : "color",
              "order": {
                "avg_price" : "asc" <a id="CO200-1"></a><i class="conum" data-value="2"></i>
              }
            },
            "aggs": {
                "avg_price": {
                    "avg": {"field": "price"} <a id="CO200-2"></a><i class="conum" data-value="1"></i>
                }
            }
        }
    }
}</pre>
</div>
<div class="sense_widget" data-snippet="snippets/300_Aggregations/50_sorting_ordering.json"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO200-2"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>计算每个桶的平均售价。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO200-1"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>桶按照计算平均值的升序排序。</p>
</td>
</tr>
</table>
</div>
<p>我们可以采用这种方式用任何度量排序，只需简单的引用度量的名字。不过有些度量会输出多个值。 <code class="literal">extended_stats</code> 度量是一个很好的例子：它输出好几个度量值。</p>

<p>如果我们想使用 多值度量(multivalue metric) 进行排序， 我们只需以关心的度量为关键词使用点式路径：</p>
<div class="pre_wrapper lang-sense">
<pre class="programlisting prettyprint lang-sense">GET /cars/transactions/_search
{
    "size" : 0,
    "aggs" : {
        "colors" : {
            "terms" : {
              "field" : "color",
              "order": {
                "stats.variance" : "asc" <a id="CO201-1"></a><i class="conum" data-value="1"></i>
              }
            },
            "aggs": {
                "stats": {
                    "extended_stats": {"field": "price"}
                }
            }
        }
    }
}</pre>
</div>
<div class="sense_widget" data-snippet="snippets/300_Aggregations/50_sorting_ordering.json"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO201-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>使用 <code class="literal">.</code> 符号，根据感兴趣的度量进行排序。</p>
</td>
</tr>
</table>
</div>
<p>在上面这个例子中，我们按每个桶的方差(variance)来排序，所以那个售价的方差最小的颜色的桶 会排在其他的颜色前面。</p>
</div>
<div class="navfooter">
<span class="prev">
<a href="_intrinsic_sorts.html">« 内置排序</a>
</span>
<span class="next">
<a href="_sorting_based_on_deep_metrics.html">基于“深度”度量排序 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>